//
//  DeploymentViewController.m
//  jboss-admin
//
//  Created by Christos Vasilakis on 29/06/2010.
//  Copyright 2010 All rights reserved.
//

#import "DeploymentsViewController.h"
#import "DeploymentDetailsRootController.h"
#import "JBossServer.h"

@implementation DeploymentsViewController

@synthesize list;
@synthesize results;

- (void)viewDidLoad { 
	[super viewDidLoad];
	
	NSMutableArray *array = [results objectForKey:@"deployment"];
	self.list = array;
	
	UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addDeployment)];
    self.navigationItem.rightBarButtonItem = addButton;

    [addButton release];
	
	NSLog(@"DeploymentsViewController viewDidLoad");
}

- (void)viewWillAppear:(BOOL)animated {
	[self.tableView reloadData];
}

- (void)viewDidUnload {
	[super viewDidUnload];
	
	self.list = nil;
	self.results = nil;
	
	NSLog(@"DeploymentsViewController viewDidUnload");
}

- (void)dealloc {
	//[list release];
	[results release];

	NSLog(@"DeploymentsViewController deAlloc");
	
	[super dealloc];
}

#pragma mark -
#pragma mark Table Data Source Methods
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	return [list count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView  cellForRowAtIndexPath:(NSIndexPath *)indexPath {
	NSUInteger row = [indexPath row];
	
	NSDictionary *deploymentDetails =  [list objectAtIndex:row];

	static NSString *CellIdentifier;
	
	if ( [[deploymentDetails objectForKey:@"status"] isEqualToString:@"STARTED"] ) {
		CellIdentifier = @"UP";
	} else {
		CellIdentifier = @"DOWN";
	}
	
	UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
	
	if (cell == nil) {
		cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
		
		UIImageView *imgView;
		
		if ( [CellIdentifier isEqualToString:@"UP"] )
			imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"up.png"]];
		else
			imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"down.png"]];			
		
		cell.accessoryView = imgView;
		
		[imgView release];
	}
	
	cell.textLabel.text = [deploymentDetails objectForKey:@"simplename"];

	return cell;
}

#pragma mark -
#pragma mark Table Delegate Methods
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
	NSUInteger row = [indexPath row];
	
	DeploymentDetailsRootController *rootController = [[DeploymentDetailsRootController alloc] initWithNibName:@"DeploymentDetailsRootController" bundle:nil];
	rootController.title = [[self.list objectAtIndex:row] objectForKey:@"simplename"];
	rootController.results = self.results;
	rootController.index = row;
	
	[self.navigationController pushViewController:rootController animated:YES];
	[rootController release];
	
	[self.tableView deselectRowAtIndexPath:indexPath animated:YES];
	
	/*
	UIAlertView *alert = [[UIAlertView alloc]
						  initWithTitle: @"Not implemented yet..."
						  message:nil
						  delegate:nil
						  cancelButtonTitle:@"Cancel"
						  otherButtonTitles:nil];
	[alert show];
	[alert release];	
*/
	
}

/*
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath {
	NSUInteger row = [indexPath row];
	
	JBossServer *server = [self.list objectAtIndex:row];
	
	ServerDetailController *detailController = [[ServerDetailController alloc] initWithStyle:UITableViewStyleGrouped];
	detailController.title = server.name;
	detailController.server = server;
	
	[self.navigationController pushViewController:detailController animated:YES];
	[detailController release];
}
*/

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
	if (editingStyle == UITableViewCellEditingStyleDelete) {
		NSUInteger row = [indexPath row];
		NSArray *paths = [NSArray arrayWithObject: [NSIndexPath indexPathForRow:row inSection:0]];
		
		[[self tableView] deleteRowsAtIndexPaths:paths withRowAnimation:UITableViewRowAnimationTop];
	}
}

#pragma mark -
#pragma mark Action Calls
- (IBAction)addDeployment {
}

- (IBAction)toggleEdit {
    BOOL editing = !self.tableView.editing;
    self.navigationItem.rightBarButtonItem.enabled = !editing;
    self.navigationItem.leftBarButtonItem.title = (editing) ? @"Done" :  @"Edit";
    [self.tableView setEditing:editing animated:YES];
}

@end